package com.gs.SynAntiWandering.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gs.SynAntiWandering.R;

public class ToolBarSearch extends LinearLayout {
    private TextView item_text;
    private TextView item_per;
    private View.OnClickListener onClickListener;

    public void setOnClickListener(View.OnClickListener onClickListener) {
        if (onClickListener != null) {
            item_per.setOnClickListener(onClickListener);
        }
    }

    public String getText() {
        return item_text.getText().toString();
    }

    public void setText(String text) {
        item_text.setText(text);
    }

    public void setHint(String hint) {
        item_text.setHint(hint);
    }

    public ToolBarSearch(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ToolBarSearch);
        LayoutInflater.from(context).inflate(R.layout.tool_bar_search, this, true);
        item_text = findViewById(R.id.item_text);
        item_per = findViewById(R.id.item_per);
        String text = attributes.getString(R.styleable.ToolBarSearch_tool_bar_search_text);
        if (text != null && !TextUtils.isEmpty(text)) {
            item_text.setText(text);
        }
        text = attributes.getString(R.styleable.ToolBarSearch_tool_bar_search_text__hint);
        if (text != null && !TextUtils.isEmpty(text)) {
            item_text.setHint(text);
        }
        text = attributes.getString(R.styleable.ToolBarSearch_tool_bar_search_per_text);
        if (text != null && !TextUtils.isEmpty(text)) {
            item_per.setText(text);
        }
        String textVsibility = attributes.getString(R.styleable.ToolBarSearch_tool_bar_search_text_visibility);
        if (textVsibility != null && !TextUtils.isEmpty(textVsibility)) {
            switch (textVsibility) {
                case "visible":
                    item_text.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_text.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_text.setVisibility(GONE);
                    break;
            }
        }
        String perVisibility = attributes.getString(R.styleable.ToolBarSearch_tool_bar_search_per_visibility);
        if (perVisibility != null && !TextUtils.isEmpty(perVisibility)) {
            switch (perVisibility) {
                case "visible":
                    item_per.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_per.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_per.setVisibility(GONE);
                    break;
            }
        }

        attributes.recycle();
    }
}